package edu.iss.javaproject.util;


import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.springframework.stereotype.Component;

import edu.iss.javaproject.exception.UserNotFoundException;
import edu.iss.javaproject.model.LeaveRecord;
	 
@Component
	public class SendMailTLS {
		private String username=null;
		private String password=null;
		private Properties props;
		private Session session;
				
		private void prepareMail()
		{
			username = "javaproject562@gmail.com";
			password = "PassWord123";
			props = new Properties();
            props.put("mail.smtp.auth", "true");
            props.put("mail.smtp.starttls.enable", "true");
            props.put("mail.smtp.host", "smtp.gmail.com");
            props.put("mail.smtp.port", "25");
            session = Session.getInstance(props,
	                  new javax.mail.Authenticator() {
	                        protected PasswordAuthentication getPasswordAuthentication() {
	                                return new PasswordAuthentication(username, password);
	                        }
	                  });
		} 
	        public  void sendMail2Empl(LeaveRecord leaveRecord) {
	        		prepareMail();
	 
	                try {
	 
	                        Message message = new MimeMessage(session);
	                        message.setFrom(new InternetAddress("javaproject562@gmail.com"));//"from-email@gmail.com"));
	                        message.setRecipients(Message.RecipientType.TO,
	                                InternetAddress.parse("javaproject562@gmail.com"));//leaveRecord.getUsers().getEmail()));
	                        message.setSubject("Leave Application Notification");
	                        message.setText("Dear "+leaveRecord.getUsers().getName()+","
	                                + "\n\n Your " + leaveRecord.getLeaveType() + " leave application from " + leaveRecord.getLeaveStart() 
	                                + " to " + leaveRecord.getLeaveEnd() + " has been " + leaveRecord.getLeaveStatus() + "."
	                                + "\n\n Thank You."
	                                + "\n\n Best Regard"
	                                + "\n\n LAPS"
	                                + "\n\n *** This is an automatically generated email, please do not reply ***"); 
	                        Transport.send(message);
	 
	                        System.out.println("Done");
	 
	                } catch (MessagingException e) {
	                        throw new RuntimeException(e);
	                }
	        }
	        public  void sendMail2Mng(LeaveRecord leaveRecord) throws UserNotFoundException {
        		prepareMail();

                try {
                	 System.out.println("test0");

                        Message message = new MimeMessage(session);
                        message.setFrom(new InternetAddress("javaproject562@gmail.com"));//"from-email@gmail.com"));
                        message.setRecipients(Message.RecipientType.TO,
                                InternetAddress.parse("javaproject562@gmail.com"));//leaveRecord.getUsers().getEmail()));
                        message.setSubject("Leave Application Notification");
                        System.out.println("test1");
                        message.setText("Dear Manager,"
                                + "\n\n Your staff, " + leaveRecord.getUsers().getName() + ", has applied for " 
                        		+ leaveRecord.getLeaveType() + " leave (" + leaveRecord.getRefNo() + ") from " + leaveRecord.getLeaveStart() 
                        		+ " to " + leaveRecord.getLeaveEnd() + " for " + leaveRecord.getDuration() + "day(s)."
                                + "\n\n Thank You."
                                + "\n\n Best Regards"
                                + "\n\n LAPS"
                                + "\n\n *** This is an automatically generated email, please do not reply ***"); 
                        System.out.println("test2");
                        Transport.send(message);
                        System.out.println("test3");
 
                        System.out.println("Done");
 
                } catch (MessagingException e) {
                        throw new RuntimeException(e);
                }
                
                
        }
}

